﻿using Microsoft.AspNetCore.SignalR;
using TrafficWebApi.Models.Response;

namespace TrafficWebApi.Services
{
    public class MessageService
    {
        private readonly IHubContext<Models.MessageHub> _MessageHub;
        public MessageService(IHubContext<Models.MessageHub> _messageHub)
        {
            this._MessageHub = _messageHub;
        }

        #region 公共发送
        /// <summary>
        /// 发送消息文本
        /// </summary>
        /// <param name="targetUserId">指定接收用户</param>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public async Task SendTextMessage(string[] targetUserId, string title, string content)
        {
            var obj = new { ticketid = "2b737ce451fa49f3b85a5399e0866a97", historyid = "1" };
            OnCommondMessage msgObj = new OnCommondMessage();
            msgObj.Content = content;
            msgObj.Title = title;
            msgObj.FunctionArgs.Add(obj);
            if (targetUserId != null && targetUserId.Length > 0)
            {
                foreach (string userId in targetUserId)
                {
                    await this._MessageHub.Clients.Groups(userId).SendAsync("SendTextMessage", msgObj);
                }
            }
            else { await this._MessageHub.Clients.All.SendAsync("SendTextMessage", msgObj); }
        }

        /// <summary>
        /// 发送消息文本 UserList
        /// </summary>
        /// <param name="Methods">指定方法</param>
        /// <param name="targetUserId">指定接收用户</param>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public async Task SendNotify(string Methods, List<string> targetUserId, string title, string content, List<object>? args = null, object? Model = null)
        {
            try
            {
                OnCommondMessage msgObj = new OnCommondMessage();
                msgObj.Content = content;
                msgObj.Title = title;
                msgObj.DoFunction = Methods;
                msgObj.FunctionArgs = args;
                msgObj.Model = Model;
                if (targetUserId != null && targetUserId.Count > 0)
                {
                    foreach (string userId in targetUserId)
                    {
                        await this._MessageHub.Clients.Groups(userId).SendAsync(Methods, msgObj);
                    }
                }
                else { await this._MessageHub.Clients.All.SendAsync(Methods, msgObj); }
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 发送消息文本 单用户
        /// </summary>
        /// <param name="Methods">指定方法</param>
        /// <param name="NotifyID">通知ID</param>
        /// <param name="targetUserId">指定接收用户</param>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public async Task SendNotify(string Methods, string NotifyID, string targetUserId, string title, string content, List<object>? args = null, object? Model = null)
        {
            try
            {
                OnCommondMessage msgObj = new OnCommondMessage();
                msgObj.NotifyID = NotifyID;
                msgObj.Content = content;
                msgObj.Title = title;
                msgObj.DoFunction = Methods;
                msgObj.Model = Model;
                msgObj.FunctionArgs = args;
                if (!string.IsNullOrEmpty(targetUserId))
                {
                    await this._MessageHub.Clients.Groups(targetUserId).SendAsync(Methods, msgObj);
                }
                //else { await this._MessageHub.Clients.All.SendAsync(Methods, msgObj); }
            }
            catch (Exception ex)
            {
                WriteLog(this.GetType().ToString() + " SendNotify " + ex.Message);
            }
        }
        #endregion

        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="msg"></param>
        public void WriteLog(string msg)
        {
            try
            {
                if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Log\\"))
                {
                    Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\Log\\");
                }
                string logPath = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\Log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".Log";
                using (System.IO.StreamWriter sw = System.IO.File.AppendText(logPath))
                {
                    sw.WriteLine(DateTime.Now.ToString("【yyyy-MM-dd HH:mm:ss】: ") + msg);
                    sw.Close();
                }
            }
            catch { }
        }

        #region H5

        #endregion
    }
}
